﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using PoolGame2.Player;
using PoolGame2.Managers;

namespace PoolGame2.Networking
{
    public class LocalNetworkPlayer : PlayerPresence
    {
        PacketReader packetReader = new PacketReader();
        PacketWriter packetWriter = new PacketWriter();

        public LocalNetworkPlayer(PlayerType type, PoolGame game)
            : base(type, game)
        {
        }

        public override void Update(GameTime gameTime)
        {
            NetworkGamer sender = null;
            PoolGame pGame = (PoolGame)Game;
            LocalNetworkGamer gamer = (LocalNetworkGamer)GetNetworkGamer();
            while (gamer.IsDataAvailable)
            {
                gamer.ReceiveData(packetReader, out sender);
                PlayerPresence player = sender.Tag as PlayerPresence;
                int type = packetReader.ReadInt32();
                ((PoolGame)(Game)).eventManager.FireEvent(new GameEvent(type, "NetworkPacket", packetReader));
                if (player != null)
                {
                    /*
                    if (type == (int)PlayerAction.PlayerActionType.ACTION_STRIKE_BALL)
                    {
                        player.AddAction(new CueBallStrikeAction(PlayerAction.PlayerActionType.ACTION_STRIKE_BALL, (float)packetReader.ReadInt32()));
                    }
                    else if (type == (int)PlayerAction.PlayerActionType.ACTION_ROTATE)
                    {
                        player.AddAction(new RotationAction(PlayerAction.PlayerActionType.ACTION_ROTATE, (float)packetReader.ReadInt32()));
                    }*/
                }
            }

            base.Update(gameTime);
        }

        public override void UpdateShot(GameTime gameTime)
        {
            /*
            PoolGame pGame = (PoolGame)Game;
            if (!pGame.gameControls.InMenu)
            {
                GameState.Rotation = pGame.mainCamera.lookAtRotation.Y;
                AddAction(new RotationAction(PlayerAction.PlayerActionType.ACTION_ROTATE, GameState.Rotation));
                if (pGame.gameControls.IsKeyPressed(Keys.Space))
                {
                    AddAction( new CueBallStrikeAction(PlayerAction.PlayerActionType.ACTION_STRIKE_BALL, 3000.0f ) );
                }
            }

            base.UpdateShot(gameTime);
             */
        }
    }
}
